package com.azhuo.core.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azhuo.core.pojo.BaseDict;
import com.azhuo.core.pojo.Customer;
import com.azhuo.core.pojo.User;
import com.azhuo.core.service.BaseDictService;
import com.azhuo.core.service.CustomerService;
import com.azhuo.utils.CreateId;
import com.azhuo.utils.Page;

@Controller
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private BaseDictService baseDictService;
	
	@Autowired
	private CreateId createId;

	@Value("${customer.from.type}")
	private String FROM_TYPE;

	@Value("${customer.industry.type}")
	private String INDUSTRY_TYPE;

	@Value("${customer.level.type}")
	private String LEVEL_TYPE;

	/**
	 * 分页查询 /{page}/{size}
	 * 
	 */
	@RequestMapping(value = "/toCustomer.action", method = RequestMethod.GET)
	public String toCustomer(Integer page, Integer size, String cust_name, String cust_source, String cust_industry,
			String cust_level, Model model) {

		// 数据库中判断了是否为null
		Customer customer = new Customer();
		if (StringUtils.isNotBlank(cust_name)) { // 去除空格后，判断是否为null、空串""
			customer.setCust_name("%" + cust_name + "%");
		}
		if (StringUtils.isNotBlank(cust_source)) { // 去除空格后，判断是否为null、空串""
			customer.setCust_source(cust_source);
		}
		if (StringUtils.isNotBlank(cust_industry)) { // 去除空格后，判断是否为null、空串""
			customer.setCust_industry(cust_industry);
		}
		if (StringUtils.isNotBlank(cust_level)) { // 去除空格后，判断是否为null、空串""
			customer.setCust_level(cust_level);
		}

		Page<Customer> result = customerService.findAllCustomer(page, size, customer);
		model.addAttribute("page", result);

		// 客户来源
		// 客户所属行业
		// 客户级别
		baseDictSelect(model);

		model.addAttribute("cust_name", cust_name);
		model.addAttribute("cust_source", cust_source);
		model.addAttribute("cust_industry", cust_industry);
		model.addAttribute("cust_level", cust_level);

		return "customer";
	}

	/**
	 * 去添加客户页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toAddCustomer.action")
	public String toAddCustomer(Model model) {

		baseDictSelect(model);

		return "addCustomer";
	}

	/**
	 * 添加客户
	 */
	@RequestMapping(value = "/addCustomer.action", method = RequestMethod.POST)
	@ResponseBody
	public String addCustomer(Customer customer, HttpServletRequest request) {
		
		customer.setCust_id(createId.createId());

		// 设置创建客户的用户id
		User user = (User) request.getSession().getAttribute("USER_SESSION");
		customer.setCust_user_id(user.getUser_id());
		customer.setCust_create_id(user.getUser_id());
		customer.setCust_createtime(new Date());
		customerService.addCustomer(customer);

		return String.valueOf(customer.getCust_id());
	}

	/**
	 * 根据id查询客户信息
	 */
	@RequestMapping(value = "/findCustomerByCust_id.action", method = RequestMethod.GET)
	public String findCustomerByCust_id(Integer cust_id, Model model) {
		System.out.println("customer = " + cust_id);

		// 根据id查询客户信息
		Customer customer = customerService.findCustomerByCust_id(cust_id);
		Page<Customer> page = new Page<Customer>();
		page.setTotal(1);
		List<Customer> rows = new ArrayList<Customer>();
		rows.add(customer);
		page.setPage(1);
		page.setSize(1);
		page.setRows(rows);

		baseDictSelect(model);
		model.addAttribute("page", page);
		return "customer";
	}

	/**
	 * 去修改客户信息页面
	 */
	@RequestMapping(value = "/toUpdateCustomerPage.action")
	public String toUpdateCustomerPage(Integer cust_id, Model model) {

		baseDictSelect(model);

		// 根据id查询客户
		Customer customer = customerService.findCustomerByCust_id(cust_id);
		model.addAttribute("customer", customer);
		return "updateCustomer";
	}

	/**
	 * 更新客户信息
	 */
	@RequestMapping(value = "/updateCustomer.action", method = RequestMethod.POST)
	@ResponseBody
	public String updateCustomer(Customer customer) {

		// 更新客户信息
		customerService.updateCustomer(customer);

		return "OK";
	}

	/**
	 * 根据cust_id删除客户信息
	 */
	@RequestMapping(value = "/deleteCustomer.action", method = RequestMethod.POST)
	@ResponseBody
	public String deleteCustomer(Integer cust_id) {
		System.out.println("cust_id = " + cust_id);

		// 根据ID删除客户信息
		int row = customerService.deleteCustomer(cust_id);
		if (row > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	// 客户来源
	// 客户所属行业
	// 客户级别
	private Model baseDictSelect(Model model) {
		// 客户来源
		List<BaseDict> fromType = baseDictService.findBaseDictByDictTypeCode(FROM_TYPE);
		// 客户所属行业
		List<BaseDict> industryType = baseDictService.findBaseDictByDictTypeCode(INDUSTRY_TYPE);
		// 客户级别
		List<BaseDict> levelType = baseDictService.findBaseDictByDictTypeCode(LEVEL_TYPE);
		model.addAttribute("fromType", fromType);
		model.addAttribute("industryType", industryType);
		model.addAttribute("levelType", levelType);
		return model;
	}
}
